package com.xiaomage.sharedpreferences_contentprovider

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.CURSOR_COLUMN_NAME
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.CURSOR_COLUMN_TYPE
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.CURSOR_COLUMN_VALUE
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.SEPARATOR
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_BOOLEAN
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_CLEAN
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_CONTAIN
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_FLOAT
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_GET_ALL
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_INT
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_LONG
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.TYPE_STRING
import com.xiaomage.sharedpreferences_contentprovider.ConstantUtil.VALUE

/**
 * Created by Administrator on 2018/4/1 0001.
 *
 * 0---key
 * 1---type
 * 2---value
 */
class SPContentProvider:ContentProvider() {
    override fun onCreate(): Boolean {
        return true
    }
    override fun query(uri: Uri?, projection: Array<out String>?, selection: String?,
                       selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
        val path=uri?.path?.split(SEPARATOR)
        val type=path?.get(1)
        if(type==TYPE_GET_ALL){
           val all= SPHelperImple.getALL(context)
            if(all!=null){
                //如果想得到一个Cursor, 而此时又没有数据库返回一个Cursor，
                // 此时可以通过MatrixCursor来返回一个Cursor
                val cursor=MatrixCursor(arrayOf(CURSOR_COLUMN_NAME,
                        CURSOR_COLUMN_TYPE,
                        CURSOR_COLUMN_VALUE))
                val keySet=all.keys
                for (key in keySet){
                    val rows= arrayOfNulls<Any>(3)
                    rows[0]=key
                    rows[2]= all[key]
                    when(rows[2]){
                        is Boolean->{
                            rows[1]=TYPE_BOOLEAN
                        }
                        is String->{
                            rows[1]= TYPE_STRING
                        }
                        is Int->{
                            rows[1]= TYPE_INT
                        }
                        is Long->{
                            rows[1]= TYPE_LONG
                        }
                        is Float->{
                            rows[1]= TYPE_FLOAT
                        }
                    }
                    cursor.addRow(rows)

                }
                return cursor
            }
        }
        return null

    }

    override fun getType(uri: Uri?): String {
        val path=uri?.path?.split(SEPARATOR)
        val type=path?.get(1)
        val value_as_key=path?.get(2)
        if(type==TYPE_CONTAIN){
            return SPHelperImple.contains(context,value_as_key).toString()
        }
        return SPHelperImple.get(context,value_as_key,type)
    }

    override fun insert(uri: Uri?, values: ContentValues?): Uri?{
        val path=uri?.path?.split(SEPARATOR)
        val type=path?.get(1)
        val value_as_key=path?.get(2)
        val obj=values?.get(VALUE)
        if(obj!=null){
            SPHelperImple.save(context,value_as_key,obj)
        }
        return null
    }

    override fun delete(uri: Uri?, selection: String?, selectionArgs: Array<out String>?): Int {
        val path=uri?.path?.split(SEPARATOR)
        val type=path?.get(1)
        if(type== TYPE_CLEAN){
            //delete all
            SPHelperImple.clear(context)
            return 0
        }
        val value_as_key=path?.get(2)
        if(SPHelperImple.contains(context,value_as_key)){
            SPHelperImple.remove(context,value_as_key)
        }
        return 0
    }


    override fun update(uri: Uri?, values: ContentValues?,
                        selection: String?, selectionArgs: Array<out String>?): Int {
        insert(uri,values)
        return 0
    }




}